package com.itdaan.portal.business.controller;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bouncycastle.util.encoders.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.itdaan.portal.business.domain.Web;
import com.itdaan.portal.business.domain.WebType;
import com.itdaan.portal.business.service.CommonService;
import com.itdaan.portal.business.service.WebService;
import com.itdaan.portal.business.service.WebTypeService;
import com.itdaan.portal.common.exception.ViewException;
import com.itdaan.portal.common.util.HtmlUtil;

/**
 * 内容查看测试类 
 * @author www.itdaan.com
 */
@Controller
@RequestMapping(value = "/")
@SessionAttributes
public class PostTestController {

    private Logger logger = LoggerFactory.getLogger(PostTestController.class);
	
	@Resource(name = "commonService")
	private CommonService commonService;
	@Resource(name = "webService")
	private WebService webService;
	@Resource(name = "webTypeService")
	private WebTypeService webTypeService;
	
	@RequestMapping(value = "/testshow.html", method = RequestMethod.GET)
    public
    ModelAndView list(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "id", required = true) Integer id,
            @RequestParam(value = "typeId", required = true) Integer typeId
    ) throws ViewException {
		ModelAndView modelAndView = new ModelAndView();
    	try{
    		WebType webType = new WebType();
    		webType.setId(typeId);
    		webType = webTypeService.findWebTypeOne(webType);
    		modelAndView.setViewName("portal/".concat(webType.getTypeNameEn()));   		
    		String tableName = "t_".concat(webType.getTypeNameEn()).concat("_test");//获取内容表的表名
    		Map<String,Object> condition = new HashMap<String, Object>();
    		condition.put("tableName", tableName);
    		condition.put("id", id);
    		condition.put("fields", "id,url,web_id as webId,title,content,post_time as postTime,author");
    		Map<String,Object> map = commonService.findOne(condition);
    		Web web = new Web();
    		web.setId(Integer.parseInt(map.get("webId").toString()));
    		web = webService.findWebOne(web);    		
    		String content = map.get("content").toString();
    		if(web.getWebLanguageId().intValue() == Web.LANGUAGE_ENGLISH){
    			content = content.substring(1);
    			content  = new String(Base64.decode(content.getBytes("UTF-8")),"UTF-8");
    		}
    		map.put("text", HtmlUtil.deleteHtmlTag(content));
    		map.put("content", content.replace("<div", "<span").replace("</div>", "</span>").replace("\t", "").replace("\n", ""));
    		map.put("webName", web.getWebName());
    		map.put("en", web.getWebLanguageId());
    		modelAndView.addObject("post", map);
    	}catch(Exception e){
			logger.error("后台业务逻辑出现异常", e);
    	}
    	return modelAndView;
    }
	
}
